<?php
$restoid=null;
if(isset($_GET["restoid"])){
	$restoid=$_GET["restoid"];
}
include("../common/common.php");
$_lang = null;
if (is_null($_lang) and isset($_COOKIE['_lang'])){
	$_lang=$_COOKIE['_lang'];
}else{
	if(is_null($_lang)){
		$_lang = FR;
	}
}

$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
$db->connect();

// Select the rows in the  table
$query = "SELECT info_resto.RESTO_NAME_$_lang AS name,
info_resto.RESTO_ADDR AS address
FROM info_resto WHERE info_resto.RESTO_ID=$restoid";
$result = $db->query_first($query);
if (!$result) {
	die('Invalid query: ' . mysql_error());
}
$db->close();

?>
<!DOCTYPE html >
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Location</title>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script type="text/javascript">

    var customIcons = {
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png',
        shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png'
      }
    };

    function load() {
    	var geocoder = new google.maps.Geocoder();
    	
    	var address = "<?= $result['address'] ?>";
    	
    	geocoder.geocode( { 'address': address}, function(results, status) {
    		
    	    if (status == google.maps.GeocoderStatus.OK) {
    	    	point = results[0].geometry.location;
    	    	showFind(point);
    	    	
    	    } else {
    	    	 showDef();
    	    }
    	  });
    }
     function  showFind(point) {
        	
          var name ="<?= $result['name'] ?>";
          var address ="<?= $result['address'] ?>";
          var type = "bar";

          var map = new google.maps.Map(document.getElementById("map"), {
              center: point,
              zoom: 13,
              mapTypeId: 'roadmap'
            });
            var infoWindow = new google.maps.InfoWindow;
          
          var html = "<b>" + name + "</b> <br/>" + address;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          });
          bindInfoWindow(marker, map, infoWindow, html);
          
        }
    function  showDef(){
        	 var map = new google.maps.Map(document.getElementById("map"), {
                 center: new google.maps.LatLng(48.759838104248047,2.2382810115814209),
                 zoom: 8,
                 mapTypeId: 'roadmap'
               });
        }
        

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

  </script>

  </head>

  <body onload="load()">
    <div id="map" style="width: 500px; height: 300px"></div>
  </body>

</html>
